//
//  MoreViewController.m
//  SSWiFi
//
//  Created by 酷安 @啥也不会啊啊啊啊啊啊  on 2024/1/24.
//

#import "MoreViewController.h"
#import "WebViewController.h"
#import "BandVC.h"
#import "AboutVC.h"
#import "BandDevicesVC.h"
#import "LoginSettingVC.h"

@interface MoreViewController ()

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initDataSource {
    self.dataSource = [[QMUIOrderedDictionary alloc] initWithKeysAndObjects:
                       @"测速", [UIImageMake(@"speed") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
//                       @"改串",[UIImageMake(@"imei") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       @"锁频",[UIImageMake(@"lock") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
//                       @"切卡",[UIImageMake(@"sim_switch") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       @"重启",[UIImageMake(@"reboot") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       @"关机",[UIImageMake(@"poweroff") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       @"web后台",[UIImageMake(@"web") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       @"登录设置",[UIImageMake(@"ip") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       @"开启adb",[UIImageMake(@"adb") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       @"关于",[UIImageMake(@"about") qmui_imageWithTintColor:[UIColor qd_titleTextColor]],
                       nil];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    self.title = @"更多";
}

- (UIViewController *)makeVcWithTitle:(NSString *)title{
    UIViewController *viewController = nil;
    if ([title isEqualToString:@"测速"]) {
        WebViewController *web = [[WebViewController alloc] init];
        NSString *urlstr = [Utils loadSpeedUrl];
        if(urlstr.qmui_trim.length == 0){
            urlstr =  @"https://test.ustc.edu.cn/";
        }
        web.webvcType = WebVCTypeSpeed;
        web.urlstr = urlstr;
        web.title = title;
        viewController = web;
    }else if([title isEqualToString:@"锁频"]){
        BandDevicesVC *vc = [[BandDevicesVC alloc] init];
        viewController = vc;
//        MJWeakSelf;
//        [[UserConfig shareInstance].api getSupportBandCallBack:^(NSArray<BandItemModel *> * _Nullable bands) {
//            if(bands.count == 0){
//                [QMUITips showError:@"该设备暂时不支持锁屏"];
//            }else{
//                [weakSelf jumpToBandVC:bands];
//            }
//        }];
//        return nil;
    }else if([title isEqualToString:@"重启"]){
        QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:nil];
        MJWeakSelf;
        QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
            [weakSelf rebootclick];
        }];
        NSString *msg = @"确定要重启设备吗?";
        QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:QMUIAlertControllerStyleAlert];
        [alertController addAction:action1];
        [alertController addAction:action2];
        [alertController showWithAnimated:YES];
        
        return nil;
    }else if([title isEqualToString:@"关机"]){
        QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleCancel handler:nil];
        MJWeakSelf;
        QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"确定" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
            [weakSelf poweroffClick];
        }];
        NSString *msg = @"确定要关机吗?";
        QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:QMUIAlertControllerStyleAlert];
        [alertController addAction:action1];
        [alertController addAction:action2];
        [alertController showWithAnimated:YES];
        
        return nil;
    }
    else if ([title isEqualToString:@"关于"]){
        AboutVC *web = [[AboutVC alloc] init];
        viewController = web;
    }else if ([title isEqualToString:@"web后台"]){
        WebViewController *web = [[WebViewController alloc] init];
        web.urlstr = [UserConfig shareInstance].api.serverHost;
        if(web.urlstr == nil){
            web.urlstr = @"http://m.home";
        }
        web.title = title;
        viewController = web;
    }else if([title isEqualToString:@"切卡"]){
       
        return  nil;
    }else if([title isEqualToString:@"开启adb"]){
        [[UserConfig shareInstance].api openAdbCallback:^(bool flag) {
            if(flag){
                [QMUITips showSucceed:@"开启adb成功"];
            }else{
                [QMUITips showError:@"开启adb失败"];
            }
        }];
        return nil;
    }else if ([title isEqualToString:@"登录设置"]){
        LoginSettingVC *vc =[[LoginSettingVC alloc] init];
        viewController = vc;
    }
    if(!viewController){
        [QMUITips showWithText:@"开发中..."];
    }
    return viewController;
}

//- (void)jumpToBandVC:(NSArray *)bands{
//    BandVC *vc = [[BandVC alloc] init];
//    vc.bands = bands;
//    [self.navigationController pushViewController:vc animated:YES];
//}

- (void)rebootclick{
    [QMUITips showLoadingInView:self.view];
    [[UserConfig shareInstance].api rebootCallback:^(bool flag, NSString * _Nonnull msg) {
        [QMUITips hideAllTips];
        [QMUITips showWithText:msg];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
    }];
}

- (void)poweroffClick{
    [QMUITips showLoadingInView:self.view];
    [[UserConfig shareInstance].api poweroffCallback:^(bool flag, NSString * _Nonnull msg) {
        [QMUITips hideAllTips];
        [QMUITips showWithText:msg];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
    }];
}

- (void)switch_sim{
    QMUIDialogSelectionViewController *dialogViewController = [[QMUIDialogSelectionViewController alloc] init];
    dialogViewController.title = @"SIM卡选择";
    NSArray *dt = @[
    @{
        @"name" : @"外置卡",
        @"value": @"0"
    },
   @{
       @"name" : @"内置卡1",
       @"value": @"1"
   },
   @{
       @"name" : @"内置卡2",
       @"value": @"2"
   },];
    
//    dialogViewController.items = [dt valueForKey:@"value"];
//    [dialogViewController setSelectedItemIndex: [[dt valueForKey:@"name"] indexOfObject:model.wifi_coverage]];
//    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
//    MJWeakSelf;
//    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogViewController *aDialogViewController) {
//        QMUIDialogSelectionViewController *d = (QMUIDialogSelectionViewController *)aDialogViewController;
//        if (d.selectedItemIndex == QMUIDialogSelectionViewControllerSelectedItemIndexNone) {
//            [QMUITips showError:@"请至少选一个" inView:d.qmui_modalPresentationViewController.view hideAfterDelay:1.2];
//            return;
//        }
//        NSString *selectname = [dt[d.selectedItemIndex] objectForKey:@"name"];;
//        [aDialogViewController hideWithAnimated:YES completion:^(BOOL finished) {
//            [weakSelf submitWiFiCover:selectname orgName:model.wifi_coverage];
//        }];
//    }];
//    [dialogViewController show];
}


- (void)didSelectCellWithTitle:(NSString *)title {
    UIViewController *viewController = [self makeVcWithTitle:title];
    if(viewController){
        [self.navigationController pushViewController:viewController animated:YES];
    }
}




@end
